/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.GUI.sidepanels;

import controller.commands.CloneCommand;
import core.BoxNode;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import model.graphic.objects.GBoxNode;

/**
 *
 * @author Jelle
 */
public class Palette extends JToolBar implements ComponentListener {

    private ArrayList<GBoxNode> gClones;
    private ArrayList<BoxNode> cClones;
    private JTextArea ta;

    public Palette() {
        super.setOrientation(1);
        this.setFloatable(false);
        this.setSize(100, 100);
        this.addComponentListener(this);
        this.setBackground(Color.WHITE);
        this.setBorder(null);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setOpaque(true);

        this.gClones = new ArrayList<GBoxNode>();
        this.cClones = new ArrayList<BoxNode>();
    }

    public void addInfoPanelRef(JTextArea ta) {
        this.ta = ta;
    }

    public void addPaletteClone(GBoxNode gb, BoxNode cb) {
        this.gClones.add(gb);
        this.cClones.add(cb);
    }

    public void addLabel(PackageItem lbl) {
        lbl.addInfoPanelRef(ta);
        lbl.setPreferredSize(new Dimension(this.getParent().getWidth() - 3, 20));
        lbl.setMinimumSize(new Dimension(this.getParent().getWidth() - 3, 20));
        lbl.setMaximumSize(new Dimension(this.getParent().getWidth() - 3, 20));
        this.add(lbl);
        updateUI();
    }

    public void addPalletteItem(PaletteItem lbl) {
        lbl.addInfoPanelRef(ta);
        this.add(lbl);
        updateUI();
    }

    public JTextArea getInfoPanelRef() {
        return ta;
    }

    public void removeLabel(PackageItem lbl) {
        this.remove(lbl);
    }

    public void removeItem(PaletteItem pi) {
        String gboxID = null, cboxID = null;
        for (Object o : pi.getCommands()) {
            if (CloneCommand.class.isAssignableFrom(o.getClass())) {
                CloneCommand comm = (CloneCommand) o;
                gboxID = comm.getGBoxID();
                cboxID = comm.getCBoxID();
            }
        }
        GBoxNode box = null;
        BoxNode cbox = null;
        for (GBoxNode gb : gClones) {
            if (gb.getId().equals(gboxID)) {
                box = gb;
            }
        }
        for (BoxNode gb : cClones) {
            if (gb.getId().equals(cboxID)) {
                cbox = gb;
            }
        }

        gClones.remove(box);
        cClones.remove(cbox);
        this.remove(pi);
    }

    public GBoxNode getGBoxNode(String id) {
        GBoxNode box = null;
        for (GBoxNode gb : gClones) {
            if (gb.getId().equals(id)) {
                box = gb;
            }
        }
        return box;
    }

    public BoxNode getCBoxNode(String id) {
        BoxNode box = null;
        for (BoxNode gb : cClones) {
            if (gb.getId().equals(id)) {
                box = gb;
            }
        }
        return box;
    }

    public void componentResized(ComponentEvent e) {
        for (Component c : this.getComponents()) {
            if (PackageItem.class.isAssignableFrom(c.getClass())) {
                c.setPreferredSize(new Dimension(this.getParent().getWidth() - 3, 20));
                c.setMinimumSize(new Dimension(this.getParent().getWidth() - 3, 20));
                c.setMaximumSize(new Dimension(this.getParent().getWidth() - 3, 20));
            }
        }
        updateUI();
    }

    public void componentMoved(ComponentEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void componentShown(ComponentEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void componentHidden(ComponentEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
